﻿using System;
using System.Collections.Generic;
using System.Text;
using System.IO;
using System.Windows.Forms;

namespace Common.Util
{
    public class FileUtil
    {
        /// <summary>
        /// 根据文件名创建对应目录
        /// </summary>
        /// <param name="file"></param>
        public static void CreateFolderByFile(string file)
        {
            int length = file.LastIndexOfAny(new char[] { '/', '\\' });
            if (length > 0)
            {
                string path = file.Substring(0, length);
                if (!Directory.Exists(path))
                {
                    Directory.CreateDirectory(path);
                }
            }
        }

        /// <summary>
        /// 读取文件，返回文件内容
        /// </summary>
        /// <param name="name"></param>
        /// <returns></returns>
        public static string ReadFile(string name)
        {
            if (File.Exists(name))
            {
                StreamReader reader = new StreamReader(name);
                return reader.ReadToEnd();
            }
            return "";
        }

        /// <summary>
        /// 读取文件，返回文件内容
        /// </summary>
        /// <param name="name"></param>
        /// <returns></returns>
        public static string[] ReadFileLine(string name)
        {
            List<string> strLines=new List<string>();

            if (File.Exists(name))
            {
                StreamReader reader = new StreamReader(name,Encoding.Default); 

                while (reader.Peek() >= 0) 
                {
                     strLines.Add( reader.ReadLine());
                }


                return strLines.ToArray();
            }
            return null;
        }

        /// <summary>
        /// 设置程序启动路径为默认路径
        /// </summary>
        public static void SetDefaultFolder()
        {
            try
            {
                Directory.SetCurrentDirectory(Application.StartupPath);
            }
            catch
            {
            }
        }

    }
}
